﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Com.Common.Cms
{
    public class PageBuilder
    {
        /// <summary>
        /// 链式分页,模仿百度分页 ^_^
        /// </summary>
        /// <param name="recordCount">数据数目</param>
        /// <param name="pageSize">每页记录条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="containerSize">一次显示的页码数目</param>
        /// <param name="linkPattern">页码连接.例如: ?page=[pageNum]</param>
        /// <param name="pageNumSymbol">页码字符串,它是linkPattern的一个子串.如:[pageNum]</param>
        /// <returns>页码实体集合</returns>
        public static LinkedList<PageNumberModel> LinkedPage(int recordCount, int pageSize, int pageIndex, int containerSize, String linkPattern, String pageNumSymbol)
        {
            if (containerSize < 3)
                containerSize = 3;
            
            //页码对象的集合
            LinkedList<PageNumberModel> list = new LinkedList<PageNumberModel>();

            #region 1.计算
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
                return list ;
            }
            int startPageNum = pageIndex - containerSize / 2;
            int endPageNum = startPageNum + containerSize - 1;
            if (startPageNum <= 0)
            {
                startPageNum = 1;
                endPageNum = containerSize;
            }

            if (endPageNum >= pageCount)
            {
                endPageNum = pageCount;
            }
            #endregion

            #region 2.将页码对象封装在一个集合中
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                //if (i > pageCount)
                //    break;
                PageNumberModel page = new PageNumberModel();
                if (i == pageIndex)
                {
                    page.IsCurrentPage = true;
                }
                else
                {
                    page.IsCurrentPage = false;
                }
                page.PageNum = i.ToString();
                int x = i;
                page.PageLink = linkPattern.Replace(pageNumSymbol, i.ToString());
                list.AddLast(page);
            }
            #endregion

            return list;
        }
        /// <summary>
        /// 计算分页数
        /// </summary>
        /// <param name="recordCount">数据数目</param>
        /// <param name="pageSize">每页显示数据条数</param>
        /// <returns>返回分页数</returns> 
        public static int GetPageCount(int recordCount, int pageSize)
        {
            return (int)Math.Ceiling((double)recordCount / pageSize);
        }
    }
    /// <summary>
    /// 页码实体类
    /// </summary>
    [System.Runtime.Serialization.DataContract]
    public class PageNumberModel
    {
        /// <summary>
        /// 页码
        /// </summary>
        [System.Runtime.Serialization.DataMemberAttribute]
        public String PageNum
        {
            set;
            get;
        }
        /// <summary>
        /// 页码链接
        /// </summary>
        [System.Runtime.Serialization.DataMemberAttribute]
        public String PageLink
        {
            set;
            get;
        }
        /// <summary>
        /// 是否是当前页
        /// </summary>
        [System.Runtime.Serialization.DataMemberAttribute]
        public bool IsCurrentPage
        {
            set;
            get;
        }
    }
}
